'use strict';

sitaApp.controller('MainCtrl', function ($scope, ngDialog, Category, Program, $window, $log, $filter, Resource) {

    console.log('Main controller start');

    $scope.categories = {};
    $scope.programs = {};
    $scope.resources = {};

    Category.listCategory({}, function success(response) {
        $scope.categories = response.data;
    }, function error() {
        $log.log("Failed to get category list");
    });

    //get all the resource if there is no parameter passed in
    Resource.listResource({"accountId": "9999999999"}, function success(response) {
        $scope.resources = response.data;
    }, function error() {
        $log.log("Failed to get resource list");
    });

    $scope.searchProgram = function () {

        $scope.playDate = $filter('date')($scope.playDate, "yyyy-MM-dd");

        var programListRequest = {"playDate": $scope.playDate};

        Program.listProgram(programListRequest).$promise.then(function (res) {
            $log.log("result is " + res);
            $scope.programs = res.data;
        }).catch(function (err) {
            $log.log("Failed to seach program");
        });
    };


    $scope.showAddCategoryPage = function () {
        ngDialog.open({
            template: 'newCategoryFormPopUp',
            controller: 'newCategoryCtrl',
            className: 'ngdialog-theme-default ngdialog-theme-custom'
        });
    };

    $scope.addProgram = function () {
        ngDialog.open({
            template: 'newProgramFormPopUp',
            controller: 'newProgramCtrl',
            className: 'ngdialog-theme-default ngdialog-program-custom'
        });
    };

    $scope.showUpdateCategoryPage = function (category) {
        ngDialog.open({
            template: 'updateCategoryFormPopUp',
            controller: 'updateCategoryCtrl',
            className: 'ngdialog-theme-default ngdialog-theme-custom',
            data: {categoryId: category.id, categoryName: category.name, categoryCover: category.cover.data}
        });
    };

    $scope.deleteCategory = function (id, name) {

        var deleteCategoryDialog = $window.confirm('你确定要删除这个类别吗?');

        if (deleteCategoryDialog) {
            var deleteRequest = {"categoryId": id, "categoryName": name};
            Category.remove(deleteRequest);
            location.reload();
        }
    }

});

sitaApp.controller('newProgramCtrl', function ($scope, $log, Program, $filter) {

    $log.log('newProgramCtrl started');

    $scope.addError = "";
    $scope.addInfo = "";

    $scope.newProgram = {};

    $scope.starttime = new Date();

    $scope.hstep = 1;
    $scope.mstep = 15;

    $scope.ismeridian = true;
    $scope.toggleMode = function () {
        $scope.ismeridian = !$scope.ismeridian;
    };

    $scope.newProgram.resourceId = "1";

    $scope.addNewProgram = function () {
        $log.log("Now will send " + JSON.stringify($scope.newProgram) + " to server");
        $scope.newProgram.playDate = $filter('date')($scope.newProgram.playDate, "yyyy-MM-dd");
        Program.addProgram(JSON.stringify($scope.newProgram)).$promise.then(function (res) {
            $log.log("result is " + res);
            $scope.addInfo = "添加节目单成功";
            $scope.newProgram = {};
            //window.location = "/tinterest/program"
            location.reload();
        }).catch(function (err) {
            $scope.addInfo = "添加节目单失败";
        });
    };

});

sitaApp.controller('updateCategoryCtrl', function ($scope, Category) {

    $scope.updateInfo = "封面必须更新，如果没有变化，请选择同样的图片,否则封面会变成空";

    console.log($scope.ngDialogData);
    $scope.updateCategory = {};


    $scope.updateCategory.categoryName = $scope.ngDialogData.categoryName;
    $scope.updateCategory.categoryCover = $scope.ngDialogData.categoryCover;
    $scope.updateCategory.categoryId = $scope.ngDialogData.categoryId;


    function validateUpdateCategory() {
        $scope.updateError = "";
        $scope.updateInfo = "";

        if ($scope.updateCategory.categoryName == "") {
            $scope.updateError = "新增分类名称不能为空";
            return false;
        } else if ($scope.updateCategory.categoryCover == "") {
            $scope.updateError = "分类封面图片不能为空";
            return false;
        }

        return true;
    };

    $scope.submitUpdate = function () {

        if (!validateUpdateCategory()) {
            $scope.updateError = "分类名称或封面不能为空";
            return;
        }

        var fd = new FormData();
        console.log($scope.updateCategory);
        var data = $scope.updateCategory;

        for (var key in data) {
            fd.append(key, data[key]);
        }

        Category.addCategory({}, fd).$promise.then(function (res) {
            $scope.updateInfo = "更新分类成功";
            $scope.newCategory = {};
            $scope.newCategory.categoryCover = {};
            location.reload();
            //window.location = "/tinterest/category"
        }).catch(function (err) {
            $scope.addInfo = "更新分类失败";
        });
    };


});


sitaApp.controller('newCategoryCtrl', function ($scope, Category) {

    function validateNewCategory() {
        $scope.addError = "";
        $scope.addInfo = "";

        if ($scope.newCategory.categoryName == "") {
            $scope.addError = "新增分类名称不能为空";
            return false;
        } else if ($scope.newCategory.categoryCover == "") {
            $scope.addError = "分类封面图片不能为空";
            return false;
        }

        return true;
    }

    $scope.addNewCategory = function () {

        if (!validateNewCategory()) {
            $scope.addError = "分类名称或封面不能为空";
            return;
        }

        var fd = new FormData();
        var data = $scope.newCategory;
        for (var key in data) {
            fd.append(key, data[key]);
        }

        Category.addCategory({}, fd).$promise.then(function (res) {
            $scope.addInfo = "添加分类成功";
            $scope.newCategory = {};
            $scope.newCategory.categoryCover = {};
            location.reload();
            //window.location = "/tinterest/category"
        }).catch(function (err) {
            $scope.addInfo = "添加分类失败";
        });
    };
});